package com.module.organization.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.poifs.storage.ListManagedBlock;
import org.hibernate.Query;
import org.hibernate.Session;

import base.dao.GenericHibernateDao;
import base.util.Page;
import base.util.Tools;

import com.module.organization.vo.Organization;
import com.module.organization.vo.OrganizationConstant;

public class OrganizationDao extends GenericHibernateDao<Organization, String> {
	static OrganizationDao dao;

	OrganizationDao() {

	}

	public static OrganizationDao getInstance() {
		if (dao == null) {
			dao = new OrganizationDao();
		}
		return dao;
	}

	public Page getList(Session session, Organization bean, Integer start,
			Integer limit) {
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		StringBuffer sb = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		if (Tools.isValid(bean.getOrganizationName())) {
			condition.append(" and obj.organizationName like ?");
			params.add(bean.getOrganizationName() + "%");
		}
		if (Tools.isValid(bean.getOrganizationType())) {
			condition.append(" and obj.organizationType =?");
			params.add(bean.getOrganizationType());
		}

		sb.append("from Organization as obj where 1=1 and state='"
				+ OrganizationConstant.NORMAL_STATE + "'  ");
		sb.append(condition);
		Query query = session.createQuery(sb.toString());
		int i = 0;
		for (Object obj : params) {
			query.setParameter(i, obj);
			i++;
		}
		query.setFirstResult(start).setMaxResults(limit);
		Query totalQuery = session
				.createQuery("select count(*) from Organization as obj where 1=1 and state='"
						+ OrganizationConstant.NORMAL_STATE + "' " + condition);
		int j = 0;
		for (Object obj : params) {
			totalQuery.setParameter(j, obj);
			j++;
		}
		page.setTotal((long) ((Long) totalQuery.uniqueResult()).intValue());
		page.setRows(query.list());
		return page;
	}

	public List justList(Session session, Organization bean) {
		StringBuffer sb = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		List<Object> params = new ArrayList<Object>();
		if (bean != null) {
			if (Tools.isValid(bean.getOrganizationName())) {
				condition.append(" and obj.organizationName like ?");
				params.add(bean.getOrganizationName() + "%");
			}
			if (Tools.isValid(bean.getOrganizationType())) {
				condition.append(" and obj.organizationType =?");
				params.add(bean.getOrganizationType());
			}
		}
		sb.append("from Organization as obj where 1=1 and state !='"
				+ OrganizationConstant.DELETE_STATE + "' ");
		sb.append(condition);
		Query query = session.createQuery(sb.toString());
		int i = 0;
		for (Object obj : params) {
			query.setParameter(i, obj);
			i++;
		}
		List list = query.list();
		return list;
	}

	public List findRootNode(Session session) {
		StringBuffer sb = new StringBuffer();
		// sb.append("from Organization as obj where 1=1 and state='"+
		// OrganizationConstant.NORMAL_STATE+"' and obj.id ='"+Pid +"'");
		sb.append("from Organization as obj where 1=1 and state='"
				+ OrganizationConstant.NORMAL_STATE + "' and obj.pid =0");
		Query query = session.createQuery(sb.toString());
		List list = query.list();
		return list;
	}

	public List<Organization> getSelfTree(Session session, String id)
			throws Exception {
		if (id == null) {
			throw new Exception();
		}
		List<Organization> list = new ArrayList<Organization>();
		List<Organization> childrenList = null;
		childrenList = this.getOrganizationTree(session, id);
		Organization selfBena = get(session, id); // 获取自已的属性
		if (selfBena != null) {
			selfBena.setChildren(childrenList);
			list.add(selfBena);
		}
		return list;
	}

	@SuppressWarnings("unchecked")
	public String getOrgNameById(Session session, String id) throws Exception {
		List<String> list = null;
		String hql = "select organizationName from Organization where id =?";
		Query query = session.createQuery(hql);
		query.setParameter(0, id);
		list = query.list();
		if (list != null && !list.isEmpty()) {
			return list.get(0);
		}
		return null;
	}

	public List<Organization> getOrganizationTree(Session session, String pid) {
		List<Organization> list = null;
		List<Organization> childrenList = null;
		Query query = session
				.createQuery("from Organization as obj where 1=1 and state !='"
						+ OrganizationConstant.DELETE_STATE + "' and obj.pid=?");
		query.setParameter(0, pid);
		list = query.list();
		for (Organization c : list) {
			childrenList = getOrganizationTree(session, c.getId());
			c.setChildren(childrenList.isEmpty() ? null : childrenList);
		}
		return list;
	}

	/**
	 * 获取当前企业可用的应用;
	 * 
	 * @param session
	 * @param entid
	 * @return
	 */
	public List getOrgCurApp(Session session, String entid) {
		List result = null;
		Query query = session
				.createQuery("SELECT bind FROM EntBind as bind WHERE bind.enterprizeId='"
						+ entid + "'");
		result = query.list();
		return result;
	}
	
	
//	public List<Organization> getIdNameList(Session session,String orgType, String orgId){
//		Query query=session.createQuery("from Organization as obj where obj.pid='"+orgId+"' and obj.organizationType='"+orgType+"'");
//		List<Organization> list=query.list();
//		return list;
//	}
	
	/**
	 * 查询
	 */
	public List<Organization> getIdNameList(Session session,String orgid){
		Query query = session.createQuery("from Organization as obj where obj.pid='"+orgid+"'");
		List<Organization> list = query.list();
		return list;
	}

}
